package com.engine.ld.utils;

import com.alibaba.fastjson.JSONObject;
import com.engine.kq.biz.KQFlowActiontBiz;
import com.engine.workflow.biz.SuperviseManagerBiz;
import com.engine.workflow.biz.requestForm.LocationFieldBiz;
import com.engine.workflow.biz.requestForm.RequestAutoApproveBiz;
import com.engine.workflow.biz.requestList.RequestAttentionBiz;
import com.engine.workflow.biz.workflowOvertime.OvertimeBiz;
import com.engine.workflow.entity.requestForm.AutoApproveEntity;
import com.engine.workflowDesign.biz.AutoForecastBiz;
import com.google.common.base.Strings;
import com.weaver.base.msgcenter.util.PoppupRemindInfoUtil;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.fna.general.FnaCommon;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.attendance.manager.HrmAttVacationManager;
import weaver.workflow.msg.MsgPushUtil;
import weaver.workflow.msg.entity.MsgOperateType;
import weaver.workflow.request.*;
import weaver.workflow.request.entity.OperateLogBean;
import weaver.workflow.request.entity.RequestOperateEntityTableNameEnum;
import weaver.workflow.request.entity.RequestOperateTypeEnum;
import weaver.workflow.request.entity.RequestOperationTableInfo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author ZhangLei
 * @date 2021/7/5
 *
 * 工作流退回
 */
public class WorkflowBackUtil {
    public int rejectRequest(User var1, int var2, boolean var3, int var4, int var5) {
        PoppupRemindInfoUtil var6 = new PoppupRemindInfoUtil();
        RecordSet var7 = new RecordSet();
        RecordSetTrans var8 = new RecordSetTrans();
        var8.setAutoCommit(false);
        RequestOperationLogManager var9 = new RequestOperationLogManager(var2);
        OperateLogBean var10 = var9.getLastOperateLog();
        RequestOperationMsgManager var12 = new RequestOperationMsgManager();
        List var13 = var12.getOperateMsg(String.valueOf(var10.getId()), MsgOperateType.DRAW_BACK);
        boolean var14 = false;
        int var15 = 0;
        var7.executeProc("workflow_Requestbase_SByID", var2 + "");
        if (var7.next()) {
            if (var7.getInt("currentnodetype") == 3) {
                var14 = true;
            }

            var15 = var7.getInt("currentnodeid");
        }

        Map var16 = var9.getTableInfos();
        RequestOperationTableInfo var17 = (RequestOperationTableInfo)var16.get(RequestOperateEntityTableNameEnum.CURRENTOPERATOR.getTableName());
        RequestOperationTableInfo var18 = (RequestOperationTableInfo)var16.get(RequestOperateEntityTableNameEnum.REQUESTLOG.getTableName());
        RequestOperationTableInfo var19 = (RequestOperationTableInfo)var16.get(RequestOperateEntityTableNameEnum.AGENTPERSONS.getTableName());
        RequestOperationTableInfo var20 = (RequestOperationTableInfo)var16.get(RequestOperateEntityTableNameEnum.REQUESTBASE.getTableName());
        RequestOperationTableInfo var21 = (RequestOperationTableInfo)var16.get(RequestOperateEntityTableNameEnum.REQUESTFLOWINFO.getTableName());
        RequestOperationTableInfo var22 = (RequestOperationTableInfo)var16.get(RequestOperateEntityTableNameEnum.REQFREENODE.getTableName());
        RequestOperationTableInfo var23 = (RequestOperationTableInfo)var16.get(RequestOperateEntityTableNameEnum.REQFREENODEGROUP.getTableName());
        RequestOperationTableInfo var24 = (RequestOperationTableInfo)var16.get(RequestOperateEntityTableNameEnum.REQSIMPLEADDSIGNLOG.getTableName());
        var7.executeProc("workflow_Requestbase_SByID", var2 + "");
        int var25 = 0;
        int var26 = 0;
        int var27 = 0;
        int var28 = 1;
        int var29 = 0;
        String var30 = "";
        if (var7.next()) {
            var25 = var7.getInt("workflowid");
            var27 = var7.getInt("currentnodeid");
            var26 = var7.getInt("currentnodetype");
        }

        var7.executeSql("select * from workflow_base where id=" + var25);
        if (var7.next()) {
            var28 = var7.getInt("isbill");
            var29 = var7.getInt("formid");
        }

        if (var28 == 1) {
            var7.executeSql("select tablename from workflow_bill where id = " + var29);
            if (var8.next()) {
                var30 = var7.getString("tablename");
            }
        }

        String var31 = "";

        String var32;
        try {
            var32 = "0";
            String var33 = "0";
            String var34 = var10.getNewRecordEntityIds(var17);
            String var35 = "select userid, usertype, nodeid, groupid, groupdetailid, wfreminduser, wfusertypes from workflow_currentoperator where requestid=" + var2 + " and " + Util.getSubINClause(var34, "id", "in");
            var7.executeSql(var35);

            while(var7.next()) {
                var6.updatePoppupRemindInfo(var7.getInt("usreid"), var7.getInt("usertype"), String.valueOf(var7.getInt("usertype") + 1), var2);
                ArrayList var36 = Util.TokenizerString(Util.null2String(var7.getString("wfreminduser")), ",");
                ArrayList var37 = Util.TokenizerString(Util.null2String(var7.getString("wfusertypes")), ",");

                for(int var38 = 0; var38 < var36.size(); ++var38) {
                    var6.updatePoppupRemindInfo(Util.getIntValue((String)var36.get(var38)), 10, (String)var37.get(var38), var2);
                }

                var32 = var32 + "," + Util.null2String(var7.getString("userid"));
                if (("," + var33 + ",").indexOf("," + Util.null2String(var7.getString("nodeid") + ",")) == -1) {
                    var33 = var33 + "," + Util.null2String(var7.getString("nodeid"));
                }
            }

            var8.executeSql("select distinct userid from workflow_currentoperator where requestid=" + var2 + " and " + Util.getSubINClause(var34, "id", "in"));

            while(var8.next()) {
                var31 = var31 + "," + var8.getString("userid");
            }

            var8.executeUpdate(" delete from Workflow_SharedScope where requestid = ? and (" + Util.getSubINClause(var34, "currentid", "in") + " or (" + Util.getSubINClause(var32, "operator", "in") + " and currentnodeid in (" + var33 + ")))", new Object[]{var2});
            var8.executeSql("delete from workflow_currentoperator where requestid=" + var2 + " and " + Util.getSubINClause(var34, "id", "in"));
            String var52 = var10.getAutoSubmitId(var17);
            if (!Strings.isNullOrEmpty(var52)) {
                var8.executeSql("delete from workflow_currentoperator where requestid=" + var2 + " and " + Util.getSubINClause(var52, "id", "in"));
            }

            var34 = var10.getNewRecordEntityIds(var18);
            String var53 = "0";
            var8.executeQuery("select logid from workflow_requestlog where (logtype='0' or logtype='2' or logtype='3' or logtype='9' or logtype='b') and requestid= ? and logid in (" + var34 + ")", new Object[]{var2});

            while(var8.next()) {
                var53 = var53 + "," + var8.getString(1);
            }

            var8.executeSql("update workflow_requestlog set logtype='1' where requestid=" + var2 + " and logid in (" + var53 + ")");
            var8.executeSql("delete from workflow_requestlog where requestid=" + var2 + " and logid in (" + var34 + ") and logid not in (" + var53 + ")");
            var34 = var10.getNewRecordEntityIds(var19);
            var8.executeSql("delete from workflow_agentpersons where requestid=" + var2 + " and groupdetailid in (" + var34 + ")");
            var34 = var10.getNewRecordEntityIds(var21);
            var8.executeSql("delete from workflow_requestflowinfo where requestid=" + var2 + " and id in (" + var34 + ")");
            var34 = var10.getNewRecordEntityIds(var22);
            var8.executeSql("delete from workflow_freenode where requestid=" + var2 + " and id in (" + var34 + ")");
            var34 = var10.getNewRecordEntityIds(var23);
            var8.executeSql("delete from workflow_freenode_group where requestid=" + var2 + " and id in (" + var34 + ")");
            List var54 = var9.getUpdateSqlByModifyLog(var10, var17);
            var54.addAll(var9.getUpdateSqlByModifyLog(var10, var18));
            var54.addAll(var9.getUpdateSqlByModifyLog(var10, var19));
            var54.addAll(var9.getUpdateSqlByModifyLog(var10, var20));
            var54.addAll(var9.getUpdateSqlByModifyLog(var10, var23));
            var54.addAll(var9.getUpdateSqlByModifyLog(var10, var24));
            Iterator var39 = var54.iterator();

            while(var39.hasNext()) {
                String var40 = (String)var39.next();
                var8.executeSql(var40.toString());
            }

            var8.executeSql("delete from workflow_nownode where requestid=" + var2);
            var8.executeSql("select a.nodetype, b.nodeattribute, a.nodeid from workflow_flownode_view a inner join workflow_nodebase_view b on a.nodeid=b.id where nodeid in (select nodeid from workflow_currentoperator where requestid=" + var2 + " and isremark=0 group by nodeid)");
            AutoApproveEntity var55 = new AutoApproveEntity();
            var55.setRst(var8);
            var55.setRequestid(var2);
            var55.setSrc("forceDrawBack");

            while(var8.next()) {
                int var56 = var8.getInt("nodeid");
                var55.setNodeid(var56);
                RequestAutoApproveBiz.delApproveLog(var55, var1);
                WFAutoApproveUtils.removeAprroveSessionInfo(var2, var56, var1.getUID());
                String var41 = "insert into workflow_nownode(requestid,nownodeid,nownodetype,nownodeattribute) values(" + var2 + "," + var8.getInt("nodeid") + "," + var8.getInt("nodetype") + "," + var8.getInt("nodeattribute") + ")";
                var8.executeSql("delete from workflow_nownode where nownodeid =" + var8.getInt("nodeid") + " and requestid=" + var2);
                var8.executeSql(var41);
            }

            (new LocationFieldBiz()).forceDrawBackLocationField(var2, var29, var28, var10, var8);
            var9.setOperateInvalid(var1, var10.getId());
            var8.commit();
            if (this.canExecuteAddInOperate(var10)) {
                try {
                    RequestCheckAddinRules var57 = new RequestCheckAddinRules();
                    var57.resetParameter();
                    var57.setTrack(false);
                    var57.setStart(false);
                    var57.setNodeid(-2);
                    var57.setRequestid(var2);
                    var57.setWorkflowid(var25);
                    var57.setObjid(-2);
                    var57.setObjtype(1);
                    var57.setIsbill(var28);
                    var57.setFormid(var29);
                    var57.setIspreadd("0");
                    var57.setUser(var1);
                    var57.setSpecialOperate(true);
                    var57.getDrawBackParams().put("nodeId", var27);
                    var57.getDrawBackParams().put("nodeType", var26);
                    var57.checkAddinRules();
                } catch (Exception var46) {

                }
            }

            (new AutoForecastBiz()).resetForecast(var2);
            this.updateIslasttimes(var2);
            (new RequestAttentionBiz()).cancelAttention(var2, "rb", (RecordSetTrans)null, var31);
            var12.setAttentionMsg(var13, var2 + "", var1.getUID() + "");
            (new MsgPushUtil()).pushMsg(var13);
        } catch (Exception var47) {
            var8.rollback();
            var47.printStackTrace();
            return -1;
        }

        try {
            RequestAddShareInfo var48 = new RequestAddShareInfo();
            var48.setRequestid(var2);
            var48.SetWorkFlowID(var25);
            var48.SetNowNodeID(var15);
            var48.SetNextNodeID(var27);
            var48.setIsbill(var28);
            User var51 = new User();
            var51.setUid(-1);
            var51.setLogintype("1");
            var48.setUser(var51);
            var48.SetIsWorkFlow(1);
            var48.setBillTableName(var30);
            var48.setHaspassnode(true);
            var48.addShareInfo();
        } catch (Exception var45) {
        }

        var7.executeSql(" update workflow_requestbase set dataaggregated = '' where requestid = " + var2);
        var7.executeProc("workflow_Requestbase_SByID", var2 + "");
        if (var7.next()) {
            var26 = var7.getInt("currentnodetype");
            var27 = var7.getInt("currentnodeid");
        }

        try {
            this.CapitalUnfreeze(String.valueOf(var2), var26, var27);
        } catch (Exception var44) {
        }

        if (var26 == 0) {
            HrmAttVacationManager manager = new HrmAttVacationManager();
            manager.handle(var2, var25, 0);

            try {
                FnaCommon var49 = new FnaCommon();
                var49.doWfForceOver(var2, 0, true);
            } catch (Exception var43) {
                (new BaseBean()).writeLog(var43);
            }

            try {
                (new KQFlowActiontBiz()).handleDrawBack(var2, var25);
            } catch (Exception var42) {
                (new BaseBean()).writeLog(var42);
            }
        }

        if (var14) {
            SuperviseManagerBiz var50 = new SuperviseManagerBiz();
            var50.findOperatorByRequestid(var1, var2);
        }

        var32 = (new BaseBean()).getPropValue("WorkflowOvertimeIsNew", "isNew");
        if ("1".equals(var32)) {
            OvertimeBiz.getInstance().addOvertimeTaskThread(var2, var25, 0);
        }

        return 1;
    }


    private boolean canExecuteAddInOperate(OperateLogBean var1) {
        if (var1.getOperateType().equals(RequestOperateTypeEnum.FORWARDREPLY.getId())) {
            return false;
        } else {
            JSONObject var2 = var1.getDetailInfo().getJSONObject(RequestOperateEntityTableNameEnum.CURRENTOPERATOR.getTableName());
            if (var2 != null) {
                String var3 = var2.getString("newIds");
                if (Strings.isNullOrEmpty(var3)) {
                    return false;
                }
            }

            if (!var1.getOperateType().equals(RequestOperateTypeEnum.REJECT.getId())) {
                RecordSet var5 = new RecordSet();
                var5.executeQuery("select groupdetailid from workflow_currentoperator where requestid = ? order by id desc", new Object[]{var1.getRequestId()});
                int var4 = -1;
                if (var5.next()) {
                    var4 = Util.getIntValue(var5.getString("groupdetailid"), -1);
                }

                if (var4 > 0) {
                    var5.executeQuery("select receivedpersons from workflow_agentpersons where requestid = ? and groupdetailid = ?", new Object[]{var1.getRequestId(), var4});
                    if (var5.next() && !Strings.isNullOrEmpty(var5.getString("receivedpersons"))) {
                        return false;
                    }
                }
            }

            return true;
        }
    }


    public void updateIslasttimes(int var1) {
        try {
            RecordSet var2 = new RecordSet();
            String var3 = "select userid, islasttimes, id, isremark from workflow_currentoperator where usertype=0 and requestid=" + var1 + " order by userid, case when isremark = 4 then 1.5 when isremark = 8 then 1.4 when isremark = 9 then 1.4 when isremark = 5 then 1.3 when isremark = 6 then 0 WHEN (isremark = 0 and takisremark = -2) THEN 2 else isremark end desc, islasttimes asc, id asc";
            ArrayList var4 = new ArrayList();
            ArrayList var5 = new ArrayList();
            ArrayList var6 = new ArrayList();
            ArrayList var7 = new ArrayList();
            ArrayList var8 = new ArrayList();
            var2.execute(var3);

            int var9;
            int var10;
            int var11;
            while(var2.next()) {
                var9 = Util.getIntValue(var2.getString("userid"), 0);
                var10 = Util.getIntValue(var2.getString("islasttimes"), 0);
                var11 = Util.getIntValue(var2.getString("id"), 0);
                if (!var8.contains(var9)) {
                    var8.add(var9);
                }

                var4.add("" + var9);
                var5.add("" + var10);
                var6.add("" + var11);
                var7.add(Util.null2String(var2.getString("isremark")));
            }

            int var12;
            for(var9 = 0; var9 < var4.size(); ++var9) {
                var10 = Util.getIntValue((String)var4.get(var9), 0);
                var11 = Util.getIntValue((String)var5.get(var9), 0);
                var12 = Util.getIntValue((String)var6.get(var9), 0);
                int var13 = Util.getIntValue((String)var7.get(var9));
                if (var9 + 1 < var4.size() && var10 == Util.getIntValue((String)var4.get(var9 + 1), 0)) {
                    if (var11 == 1) {
                        var3 = "update workflow_currentoperator set islasttimes=0 where id=" + var12;
                        var2.execute(var3);
                    }
                } else if (var11 == 0) {
                    var3 = "update workflow_currentoperator set islasttimes=1 where id=" + var12;
                    var2.execute(var3);
                }
            }

            Iterator var15 = var8.iterator();

            label59:
            while(var15.hasNext()) {
                var10 = (Integer)var15.next();
                var11 = 0;
                var2.executeQuery("select id,isremark,takisremark from workflow_currentoperator where islasttimes=0 and userid=? order by id", new Object[]{var10});

                while(true) {
                    String var16;
                    do {
                        do {
                            if (!var2.next()) {
                                if (var11 > 0) {
                                    var2.executeUpdate("update workflow_currentoperator set islasttimes=0 where requestid=? and userid=? and islasttimes=1", new Object[]{var1, var10});
                                    var2.executeUpdate("update workflow_currentoperator set islasttimes=1 where id=?", new Object[]{var11});
                                }
                                continue label59;
                            }

                            var12 = var2.getInt("id");
                            var16 = var2.getString("takisremark");
                        } while(var12 != 0);
                    } while(!"".equals(var16) && !"0".equals(var16));

                    var11 = var2.getInt("id");
                }
            }
        } catch (Exception var14) {
        }

    }


    public void CapitalUnfreeze(String var1, int var2, int var3) {
        RecordSet var4 = new RecordSet();
        RecordSet var5 = new RecordSet();
        var4.executeSql(" select r.requestid,r.workflowid,r.currentnodetype from workflow_requestbase r,workflow_base b where requestid = " + var1 + " and r.workflowid=b.id and b.formid=19 and b.isbill=1");

        label47:
        while(var4.next()) {
            String var6 = " select b.* from workflow_form w,bill_CptFetchDetail b where w.requestid =" + var1 + " and w.billid=b.cptfetchid";
            var5.executeSql(var6);
            RecordSet var7 = new RecordSet();

            while(true) {
                while(true) {
                    if (!var5.next()) {
                        continue label47;
                    }

                    String var8 = var5.getString("capitalid");
                    float var9 = 0.0F;
                    float var10 = 0.0F;
                    float var11 = 0.0F;
                    var7.executeSql("select number_n as old_number_n from bill_CptFetchDetail where cptfetchid = (select id from bill_CptFetchMain where requestid=" + var1 + ") and capitalid=" + var8);
                    if (var7.next()) {
                        var9 = var7.getFloat("old_number_n");
                    }

                    var7.executeSql("select frozennum as old_frozennum from CptCapital where id=" + var8);
                    if (var7.next()) {
                        var10 = var7.getFloat("old_frozennum");
                    }

                    if (var3 == 0 && (var2 == 1 || var2 == 2)) {
                        var11 = var10 - var9;
                        var7.executeSql("update CptCapital set frozennum=" + var11 + " where id=" + var8);
                    } else if (var2 == 0 && (var3 == 1 || var3 == 2)) {
                        var11 = var10 + var9;
                        var7.executeSql("update CptCapital set frozennum=" + var11 + " where id=" + var8);
                    }
                }
            }
        }

    }
}
